﻿#include<fstream>
#include<iostream>
#include<algorithm>
#include "State.h"

using namespace std;


void readFile(char** buffer, int* size, const char* filename) {
	ifstream in(filename);
	if (!in) {
		*buffer = 0;
		*size = 0;
	}
	else {
		in.seekg(0, ifstream::end);
		*size = static_cast<int>(in.tellg());
		in.seekg(0, ifstream::beg);
		*buffer = new char[*size];
		in.read(*buffer, *size);
	}
}

int main(int argc, char** argv) {
	const char* filename = "stageData.txt";
	if (argc >= 2) {
		filename = argv[1];
	}
	char* stageData;
	int fileSize;
	readFile(&stageData, &fileSize, filename);

	if (!stageData) {
		cout << "stage file could not be red." << endl;
		return 1;
	}

	State* state = new State(stageData, fileSize);

	while (true) {
		state->draw();
		if (state->hasCleared()) {
			break;
		}
		cout << "a:left d:right w:up s:down. command?" << endl;
		char input;
		cin >> input;
		state->update(input);
	}
	cout << "Congratulation's! You won." << endl;
	delete[] stageData;
	stageData = 0;

	cin.get();
	cin.get();
}

